﻿Imports Bangbezh.Core
Imports System.Drawing.Drawing2D

Public Class FrmMain
    'To move with titlebar
    Dim mouseOffset As Point

    Dim max As Integer = 100
    Dim min As Integer = 0
    Dim val As Integer = 0
    Dim valFix As Integer = 0

    Dim B As Integer = 100
    Dim G As Integer = 100
    Dim R As Integer = 100
    Dim RemTime As TimeSpan

    Private Sub FrmMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Tray.Visible = False
    End Sub
    Protected Overrides Sub OnPaintBackground(e As PaintEventArgs)
        If System.Windows.Forms.SystemInformation.HighContrast Then
            CtxtBangbezh.BackColor = Color.FromArgb(100, 100, 100)
        Else
            CtxtBangbezh.BackColor = Color.White
        End If

        MyBase.OnPaintBackground(e)
    End Sub
    Private Sub FrmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        If FileIO.FileSystem.FileExists(GetStartupPath() & "\$&!@#tmp.tmp") Then
            FileIO.FileSystem.DeleteFile(GetStartupPath() & "\$&!@#tmp.tmp")
        End If
        'Get Options
        Dim BangbezhBBJ As New Ini 'Make a new BBJ File for bangbezh in memory
        Dim OptionsBBJ As New Ini 'Make a new BBJ File for options in memory

        Try
            Check()
            OptionsBBJ.Load(Application.StartupPath & "\Data\Options.Bbj") 'Load Data into it
            BangbezhFile = OptionsBBJ.GetStringField("Options", "Bangbezh")
            City = CInt(OptionsBBJ.GetNumberField("Options", "City")) 'Get City
            'Get Bangbezh's Color
            MyColor = Colors(CShort(OptionsBBJ.GetNumberField("Options", "Color"))) 'Get First color
            ChangeColor() 'Apply The Color
            'Set Bangbezh's Volume
            Volume = CShort(OptionsBBJ.GetNumberField("Options", "Volume"))
            'Decide wether TmrAzkar Should be enabled
            TmrAzkar.Enabled = OptionsBBJ.GetBooleanField("Options", "Zikr")
            'Set FixMins
            Fix = OptionsBBJ.GetBooleanField("Options", "Fix")
            If Fix Then
                Select Case OptionsBBJ.GetNumberField("Options", "FixMins")
                    Case 0
                        FixMins = New TimeSpan(0, 5, 0)
                    Case 1
                        FixMins = New TimeSpan(0, 10, 0)
                    Case 2
                        FixMins = New TimeSpan(0, 15, 0)
                End Select
            Else
                FixMins = New TimeSpan(0, 0, 0)
            End If

        Catch ex As InvalidOperationException
            If ex.Message.Contains(GetStartupPath() & "\Data\Options.bbj") Then
                WriteOptions()
                MsgBox("ڕێكخستنەكان داندرانەوە.", vbInformation, "دانانەوەی ڕێكخستنەكان")
                Me.Close()
                Process.Start(Application.StartupPath & "\Bangbezh.exe")
            Else
                Throw ex
            End If

        End Try

        'Get the Prayer Times from the external file.
        If UseTray = False Then
            Core.Load()
            Core.GetPrayerTimes()
        End If
        If City < 5 Then
            LblCityName.Text = Cities(City)
        End If
        'Glow BangbezhMnu and SettingsToolstrip if it was the first time the user has opened Bangbezh.
        If My.Settings.First Then
            TmrFrst.Enabled = True
            FrmLike.ShowDialog()
        End If

    End Sub
    Dim x As Integer = 0
    Dim ShowFix As Boolean = False
    Private Sub TmrPrayers_Tick(sender As Object, e As EventArgs) Handles TmrPrayers.Tick

        On Error Resume Next
        If x < 5 Then
            x += 1
        Else
            ShowFix = True
        End If

        If TmrPrayers.Interval = 1000 Then
            TmrPrayers.Interval = 500
        End If
        Core.GetNextPrayer() 'Get The Next prayer
        valFix = CInt(FixMins.TotalSeconds)
        Dim percent As Decimal = CDec((val - min) / (max - min)) 'Percentage of progressbar
        Dim PercentFix As Decimal = CDec((valFix - min) / (max - min))
        'Show prayertimes in lacale culture
        LblFajr.Text = FajrD.ToString("hh:mm")
        LblShruq.Text = ShruqD.ToString("hh:mm")
        LblDhuhr.Text = DhuhrD.ToString("hh:mm")
        lblAsr.Text = AsrD.ToString("hh:mm")
        lblMaghrib.Text = MaghribD.ToString("hh:mm")
        lblIsha.Text = IshaD.ToString("hh:mm")


        Select Case NxtPrayer
            Case 0 'If NxtPrayer= 0
                LblNextPr.Text = "ماوە بۆ " + Prayers(NxtPrayer)  'LblNextPr's text = "بەیانی"
                LblRemtime.Text = GetRemainingTime(Math.Abs(Rem2Fajr))  'Set lblRemtime.text
                Dim tmspan As New TimeSpan(0, 0, Math.Abs(Rem2Fajr))
                lblremtmSecond.Text = ChangeFormat(tmspan.Seconds)
                max = Math.Abs(MaxFajr) 'setmax val of progress bar
                val = Math.Abs(Math.Abs(MaxFajr) - Math.Abs(Rem2Fajr)) 'set val of progress bar
            Case 1
                LblNextPr.Text = "ماوە بۆ " + Prayers(NxtPrayer + 1)
                LblRemtime.Text = GetRemainingTime(Math.Abs(Rem2Dhuhr))
                Dim tmspan As New TimeSpan(0, 0, Math.Abs(Rem2Dhuhr))
                lblremtmSecond.Text = ChangeFormat(tmspan.Seconds)
                max = Math.Abs(MaxDhuhr)
                val = Math.Abs(Math.Abs(MaxDhuhr) - Math.Abs(Rem2Dhuhr))

            Case 2
                LblNextPr.Text = "ماوە بۆ " + Prayers(NxtPrayer)
                LblRemtime.Text = GetRemainingTime(Math.Abs(Rem2Dhuhr))
                Dim tmspan As New TimeSpan(0, 0, Math.Abs(Rem2Dhuhr))
                lblremtmSecond.Text = ChangeFormat(tmspan.Seconds)
                max = Math.Abs(MaxDhuhr)
                val = Math.Abs(Math.Abs(MaxDhuhr) - Math.Abs(Rem2Dhuhr))
            Case 3
                LblNextPr.Text = "ماوە بۆ " + Prayers(NxtPrayer)
                LblRemtime.Text = GetRemainingTime(Math.Abs(Rem2Asr))
                Dim tmspan As New TimeSpan(0, 0, Math.Abs(Rem2Asr))
                lblremtmSecond.Text = ChangeFormat(tmspan.Seconds)
                max = Math.Abs(MaxAsr)
                val = Math.Abs(Math.Abs(MaxAsr) - Math.Abs(Rem2Asr))

            Case 4
                LblNextPr.Text = "ماوە بۆ " + Prayers(NxtPrayer)
                LblRemtime.Text = GetRemainingTime(Math.Abs(Rem2Maghrib))
                Dim tmspan As New TimeSpan(0, 0, Math.Abs(Rem2Maghrib))
                lblremtmSecond.Text = ChangeFormat(tmspan.Seconds)
                max = Math.Abs(MaxMaghrib)
                val = Math.Abs(Math.Abs(MaxMaghrib) - Math.Abs(Rem2Maghrib))

            Case 5
                LblNextPr.Text = "ماوە بۆ " + Prayers(NxtPrayer)
                LblRemtime.Text = GetRemainingTime(Math.Abs(Rem2Isha))
                Dim tmspan As New TimeSpan(0, 0, Math.Abs(Rem2Isha))
                lblremtmSecond.Text = ChangeFormat(tmspan.Seconds)
                max = Math.Abs(MaxIsha)
                val = Math.Abs(Math.Abs(MaxIsha) - Math.Abs(Rem2Isha))
        End Select
        If Fix = True Then
            PicPrgrss.Height = CInt(BackPicPrgrss.Height - ((BackPicPrgrss.Height - 2) _
            * PercentFix) - 2 - ((BackPicPrgrss.Height + ((BackPicPrgrss.Height - 2) _
            * PercentFix) - 2) * percent)) 'Show how much is remaining for the next prayer
        Else
            PicPrgrss.Height = CInt(BackPicPrgrss.Height - 2 - ((BackPicPrgrss.Height - 2) * percent))
        End If
        PicPrgrss.Top = BackPicPrgrss.Bottom - PicPrgrss.Height - 1
        PicFix.Top = BackPicPrgrss.Bottom - 1 - PicFix.Height
        If ShowFix Then
            If Fix = True Then
                If PicPrgrss.Height = 0 Then
                    If LblRemtime.Text.Contains("00:") Then
                        Dim NowMinutes As Integer = CInt((Replace(LblRemtime.Text, "00:", "")))
                        Dim nowSeconds As Integer = CInt(lblremtmSecond.Text)
                        PicFix.Height = CInt(((BackPicPrgrss.Height - 2) * PercentFix) / _
                            (FixMins.TotalSeconds / New TimeSpan(0, NowMinutes, nowSeconds).TotalSeconds))
                        PicFix.Top = BackPicPrgrss.Bottom - 1 - PicFix.Height
                    End If
                Else
                    PicFix.Height = CInt((BackPicPrgrss.Height - 2) * PercentFix)
                    PicFix.Top = PicPrgrss.Top - PicFix.Height
                End If

                PicFix.Visible = True
                'If it's the time for prayer, then Call for it :)
            End If
            If Rem2PrNxt = 0 Then
                Bangda()
            End If
        End If
        Dim fullFilePath As String
        Dim fileContents As String

        If My.Computer.FileSystem.FileExists(TempFileName) Then
            FileIO.FileSystem.DeleteFile(TempFileName)
            ShowToolStripMenuItem.PerformClick()
            TempFileName = Nothing
        End If
    End Sub

    Function GetRemainingTime(ByVal remainingtime As Long) As String
        Dim remtm As TimeSpan = New TimeSpan(0, 0, CInt(remainingtime))
        RemTime = remtm
        Return ChangeFormat(remtm.Hours) & ":" & ChangeFormat(remtm.Minutes)
    End Function

    Private Sub BtnHide_Click(sender As Object, e As EventArgs) Handles BtnHide.Click
        Me.Hide()

        If My.Settings.WhereIsBangbezh > 0 Then
            Tray.ShowBalloonTip(1000, "بانگبێژ", "! بانگبێژ لێرەیە", ToolTipIcon.Info)
            My.Settings.WhereIsBangbezh -= 1
            My.Settings.Save()
        End If
        Tray.Visible = True
    End Sub


    Private Sub BangbezhMnu_Click(sender As Object, e As EventArgs) Handles BangbezhMnu.Click
        CtxtBangbezh.Show(BangbezhMnu, 40 - CtxtBangbezh.Width, 40)
    End Sub

    ''' <summary>
    ''' Changes the colors of the program
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ChangeColor()
        BangbezhMnu.BackColor = Color.Transparent
        BangbezhMnu.FlatAppearance.MouseOverBackColor = System.Windows.Forms.ControlPaint.Light(MyColor)
        BtnHide.FlatAppearance.MouseOverBackColor = System.Windows.Forms.ControlPaint.Light(MyColor)
        BtnMore.FlatAppearance.MouseOverBackColor = MyColor
        PicPrgrss.BackColor = MyColor
        PnlTitle.BackColor = MyColor
        PicFix.BackColor = System.Windows.Forms.ControlPaint.Light(MyColor)
        LblCityName.ForeColor = System.Windows.Forms.ControlPaint.LightLight(MyColor)
        BtnMore.BackColor = ControlPaint.Light(MyColor)
    End Sub

    Private Sub SettingsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SettingsToolStripMenuItem.Click
        'If it was the first time the user opens banbezh
        My.Settings.First = False
        My.Settings.Save()
        TmrFrst.Enabled = False
        SettingsToolStripMenuItem.ForeColor = Color.FromArgb(46, 46, 46)
        SettingsToolStripMenuItem.Font = New Font(SettingsToolStripMenuItem.Font.FontFamily _
        , SettingsToolStripMenuItem.Font.Size, FontStyle.Regular)

        BangbezhMnu.BackColor = PnlTitle.BackColor
        Me.Cursor = Cursors.WaitCursor
        'DO your REAL job
        FrmSettings.Show(Me)
        FrmSettings.Left = CInt(Me.Left - (Me.Width / 2))
        FrmSettings.Top = CInt(Me.Top + (Me.Height / 2))
        FrmSettings.Top = CInt(FrmSettings.Top - (FrmSettings.Height / 2))
        FrmSettings.TabControl1.SelectTab(0)
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub ShowToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ShowToolStripMenuItem.Click
        Me.ShowInTaskbar = True
        Me.WindowState = FormWindowState.Normal
        Me.Show()
    End Sub

    Sub Quit()
        Application.Exit()
    End Sub

    Private Sub ExitToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem1.Click, ExitToolStripMenuItem.Click
        Quit()
    End Sub

    Private Sub AboutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem.Click, AboutToolStripMenuItem1.Click

        Me.Show()
        DlgAbout.ShowDialog()
    End Sub

    Private Sub Tray_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles Tray.MouseDoubleClick
        ShowToolStripMenuItem.PerformClick()
    End Sub

    Private Sub TmrFrst_Tick(sender As Object, e As EventArgs) Handles TmrFrst.Tick
        If B < 230 Then
            B += 5
        Else
            B = 20
        End If
        'Apply the changes.

        BangbezhMnu.BackColor = Color.FromArgb(0, 0, B)
        SettingsToolStripMenuItem.ForeColor = Color.FromArgb(0, 0, B)
        SettingsToolStripMenuItem.Font = New Font(SettingsToolStripMenuItem.Font.FontFamily _
        , SettingsToolStripMenuItem.Font.Size, FontStyle.Bold)

    End Sub
    Dim generator As New Random
    Dim randomValue As Integer
    Private Sub TmrAzkar_Tick(sender As Object, e As EventArgs) Handles TmrAzkar.Tick
        Dim Azkar As String() = {"الله اكبر", "لا اله الا الله", "الحمد لله", "سبحان الله", _
                              "استغفر الله", "لاحول ولا قوة الا بالله", "اللهم صلي علی سيدنا محمد"}

        randomValue = generator.Next(0, Azkar.Length - 1)
        Tray.ShowBalloonTip(100, "یادی خودا", Azkar(randomValue), ToolTipIcon.None)
    End Sub

    Private Sub HelpToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HelpToolStripMenuItem.Click
        Process.Start(Website & "/p/yarmati.html")
    End Sub

    Private Sub PicFix_MouseHover(sender As Object, e As EventArgs) Handles PicFix.MouseHover
        TheToolTip.Show(" كاتی بانگ " & HinduNumeric(CStr(FixMins.TotalMinutes)) & " خولەك هێنراوەتە پێش ", PicFix)
    End Sub

    Private Sub BtnMore_Click(sender As Object, e As EventArgs) Handles BtnMore.Click
        FrmDetails.ShowDialog()
    End Sub

#Region "MOVE THE FORM"

    Private Sub Me_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown, PnlTitle.MouseDown
        mouseOffset = New Point(-e.X, -e.Y)
    End Sub

    Private Sub Me_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove, PnlTitle.MouseMove
        If e.Button = MouseButtons.Left Then
            Dim mousePos = Control.MousePosition
            mousePos.Offset((mouseOffset.X), (mouseOffset.Y))
            Location = mousePos
        End If
    End Sub
    Private Sub LblTitle_MouseDown(sender As Object, e As MouseEventArgs) Handles LblTitle.MouseDown
        mouseOffset = New Point(-e.X, -e.Y)
    End Sub

    Private Sub LBLTitle_MouseMove(sender As Object, e As MouseEventArgs) Handles LblTitle.MouseMove
        'If Left button is pressed
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim mousepos = Control.MousePosition
            mousepos.Offset(mouseOffset.X + (-Me.Width + 210), mouseOffset.Y)
            Location = mousepos 'Change Location of Bangbezh to mousepos
        End If
    End Sub
    Private Sub Controls_MouseDown(sender As Object, e As MouseEventArgs) Handles Label1.MouseDown, _
    Label2.MouseDown, Label3.MouseDown, Label4.MouseDown, Label5.MouseDown, Label6.MouseDown, _
    LblFajr.MouseDown, LblShruq.MouseDown, LblDhuhr.MouseDown, Label8.MouseDown, Label7.MouseDown
        mouseOffset = New Point(-e.X, -e.Y)
    End Sub

    Private Sub Controls_MouseMove(sender As Object, e As MouseEventArgs) Handles Label1.MouseMove, _
        Label2.MouseMove, Label3.MouseMove, _
        LblFajr.MouseMove, LblShruq.MouseMove, LblDhuhr.MouseMove, Label8.MouseMove, Label7.MouseMove
        'If Left button is pressed
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Location = Me.PointToScreen(New Point(e.X - 38, e.Y - 16)) 'Change Location of Bangbezh to mousepos
        End If

    End Sub
#End Region

    Private Sub LblCityName_Click(sender As Object, e As EventArgs) Handles LblCityName.Click
        SettingsToolStripMenuItem.PerformClick()
    End Sub
    Dim FntSz As Double = 0
    Private Sub TmrHideCity_Tick(sender As Object, e As EventArgs) Handles TmrHideCity.Tick
        TmrHideCity.Interval = 1
        If LblTitle.Top < 9 Then
            LblTitle.Top += 1
        Else
            LblCityName.Visible = False
            TmrHideCity.Enabled = False
        End If
    End Sub

    Private Sub BtnMore_MouseLeave(sender As Object, e As EventArgs) Handles BtnMore.MouseLeave
        BtnMore.ForeColor = Color.White
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs)
        DlgUnhandledError.Show()
    End Sub

End Class
